package oktx.narayana;

import com.arjuna.ats.jdbc.TransactionalDriver;
import org.springframework.boot.jta.narayana.NarayanaDataSourceBean;

import javax.sql.XADataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by huangdachao on 2018/6/5 17:01.
 */
public class NarayanaDataSource extends NarayanaDataSourceBean {
    private final XADataSource xaDataSource;

    public NarayanaDataSource(XADataSource xaDataSource) {
        super(xaDataSource);
        this.xaDataSource = xaDataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        Properties properties = new Properties();
        properties.put(TransactionalDriver.XADataSource, this.xaDataSource);
        return NarayanaConnectionManager.create(null, properties);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        Properties properties = new Properties();
        properties.put(TransactionalDriver.XADataSource, this.xaDataSource);
        properties.put(TransactionalDriver.userName, username);
        properties.put(TransactionalDriver.password, password);
        return NarayanaConnectionManager.create(null, properties);
    }
}
